﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using Photograp.Models;
using System.Web.Mvc;
using System.IO;

namespace Photograp.Controllers
{
    public class PhotoController : ApiController
    {
        private PhotographContext db = new PhotographContext();
        
        //POST api/Album/5/SendPosition
        [System.Web.Http.HttpPost]
        [System.Web.Mvc.ActionName("DefaultPhotoApi")]
        public HttpResponseMessage SendPositionPhoto(int id, List<SortedPhoto> positions)
        {
            Album album = db.Albums.Include(p => p.Photos).FirstOrDefault(p => p.AlbumId == id);

            if (album == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }
            else
            {
                foreach (var pos in positions)
                {
                    var photo = album.Photos.FirstOrDefault(p => p.PhotoId == pos.PhotoId);
                    if (photo != null)
                        photo.Position = pos.Position;
                }
                db.SaveChanges();
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }
    }
}
